package com.xt.base.util.json;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class JsonCalendarValueProcessor implements JsonValueProcessor{
	private String format = "yyyy-MM-dd HH:mm:ss";
	public JsonCalendarValueProcessor(){}
	public JsonCalendarValueProcessor(String format){
		this.format = format;
	}

	public Object processArrayValue(Object value,JsonConfig jsonConfig){
		String[] obj = {};
		if(value instanceof Date[]){
			SimpleDateFormat sf = new SimpleDateFormat(format);
			Calendar[] cals = (Calendar[])value;
			obj = new String[cals.length];
			for(int i=0;i<cals.length;i++){
				obj[i] = sf.format(cals[i].getTime());
			}
		}
		
		return obj;
	}
	
	public Object processObjectValue(String key,Object value,
			JsonConfig jsonConfig){
		if(value instanceof Calendar){
			String str = new SimpleDateFormat(format).format(((Calendar)value).getTime());
			return str;
		}
		return value == null ? null : value.toString();
	}
	public String getFormat() {
		return format;
	}
	public void setFormat(String format) {
		this.format = format;
	}
	
}
